var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    searchVal: "",
    url: '',
    access:[],
    showModal: false,
    userInfo:null,
    applyAmount:'',
    hospital:'',
    accessoryName:'',
    stockNum:0,
    accessoryBrandName:'',
    accessoryModel:'',
    accessorySize:'',
    equipmentSerial:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: getApp().globalData.url,
      userInfo: getApp().globalData.yukaiUser
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  searchParts: function(){
    wx.showLoading({});
    var userInfo = this.data.userInfo ? this.data.userInfo : getApp().getUser();
    var url = this.data.url ? this.data.url : getApp().getUrl();
    var _self = this;
    wx.request({
      url: url + '/accessoryApplyOrder/queryInventory',
      data: {
        token:userInfo.token,
        name: this.data.searchVal
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        switch(data.code){
          case 200:
            _self.setData({
              access: data.obj
            });
            wx.hideLoading();
          break;
          case 412:
            wx.hideLoading();
            utils.alertBox(data.msg);
            break;
          case 422:
          wx.hideLoading();
          utils.toLogin();
          break;
          default:
          wx.hideLoading();
          break;
        }
      },
      fail: function () {
        wx.hideLoading();
        utils.alertBox('请求失败，请检查网络');
      }
    });
  },
  clearInput: function () {
    this.setData({
      searchVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
  },
  //跳转至申请页面
  showApply:function(e){
    console.log(e);
    // wx.navigateTo({
    //   url: '',
    // })
  },
  /**
   * 弹窗
   */
  showDialogBtn: function (e) {
    this.setData({
      showModal: true,
      hospital: e.currentTarget.dataset.parts.hospitalName,
      accessoryName: e.currentTarget.dataset.parts.accessoryName,
      stockNum: e.currentTarget.dataset.parts.amount,
      accessoryModel: e.currentTarget.dataset.parts.accessoryModel,
      accessoryBrandName: e.currentTarget.dataset.parts.accessoryBrandName,
      accessorySize: e.currentTarget.dataset.parts.accessorySize
    });
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false,
      equipmentSerial:'',
      applyAmount:''
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  amountChange: function(e){
    this.setData({
      applyAmount: e.detail.value
    });
  },
  inputSerial: function(e){
    this.setData({
      equipmentSerial: e.detail.value
    });
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function (obj) {
    var _self = this;
    wx.showModal({
      title: '提示',
      content: '是否申请配件？',
      success: function (res) {
        if (res.confirm) {
          if (!utils.reg.isNull(_self.data.serial)){
            utils.alertBox('请将信息填写完整');
          }else{
            if (!utils.reg.isNum(_self.data.applyAmount)) {
              utils.alertBox('请输入正确数量');
            } else {
              if (_self.data.applyAmount > _self.data.stockNum) {
                utils.alertBox('库存数量不足');
              } else {
                wx.showLoading({});
                var userInfo = _self.data.userInfo;
                var data = {
                  token: userInfo.token,
                  staffId: userInfo.staffId,
                  applyPeople: userInfo.name,
                  applyTel: userInfo.phone,
                  accessoryName: _self.data.accessoryName,
                  accessoryBrandName: _self.data.accessoryBrandName,
                  accessoryModel: _self.data.accessoryModel,
                  accessorySize: _self.data.accessorySize,
                  equipmentSerial: _self.data.equipmentSerial,
                  applyAmount: _self.data.applyAmount,
                  hospitalName: _self.data.hospital
                }; 
                wx.request({
                  url: _self.data.url + '/accessoryApplyOrder/add',
                  data: data,
                  method: 'POST',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    console.log(res.data);
                    var data = res.data;
                    switch (data.code) {
                      case 200:
                        wx.hideLoading();
                        _self.hideModal();
                        utils.alertBox('申请成功');
                        break;
                      case 412:
                        wx.hideLoading();
                        utils.alertBox(data.msg);
                        break;
                      case 422:
                        wx.hideLoading();
                        utils.toLogin();
                        break;
                      default:
                        wx.hideLoading();
                        break;
                    }
                  },
                  fail: function () {
                    wx.hideLoading();
                    utils.alertBox('请求失败，请检查网络');
                  }
                });
              }
            }
          }
        }
      }
    })
  },
  //重置数据
  resetData: function(){
    this.setData({
      inputShowed: false,
      searchVal: "",
      access: [],
      showModal: false,
      applyAmount: '',
      serial:'',
      hospital: '',
      accessoryName: '',
      stockNum: 0
    });
  }
})